﻿using CubeCube.Chunk;
using CubeCube.Configuration;
using CubeCube.Gameplay;
using CubeCube.Sector;
using CubeCube.Tile;
using CubeCube.Utils;
using CubeCube.World.Generator;
using System;

namespace CubeCube.World
{
    public class CubeWorld
    {
        public const string VERSION_INFO = "CubeCue-V1.0";
        public ChunkManager chunkManager;
        public Config config;

        public IObjectListener objectListener;
        public IFxListener fxListener;
        public IFileOperator fileOperator;
        public string worldName;

        public BaseGameplay gameplay;

        public Random randomGenerator;

        /*  地图边界参数  */
        public int sizeX;
        public int sizeY;
        public int sizeZ;

        public int LeftX
        {
            get
            {
                return -(sizeX >> 1); 
            }
        }
        public int RightX
        {
            get
            {
                return (sizeX >> 1) - 1;
            }
        }
        public int TopY
        {
            get
            {
                return sizeY - 1;
            }
        }
        public int BottomY
        {
            get
            {
                return 0;
            }
        }
        public int BackZ
        {
            get
            {
                return -(sizeZ >> 1);
            }
        }
        public int FrontZ
        {
            get
            {
                return (sizeZ >> 1) - 1;
            }
        }

        private Vector playerPosition;
        public Vector PlayerPosition
        {
            get
            {
                return playerPosition;
            }
            set
            {
                playerPosition = value;
            }
        }


        public CubeWorld(IObjectListener objectListener, IFxListener fxListener, IFileOperator fileOperator,string worldName, int randomSeed)
        {
            this.objectListener = objectListener;
            this.fxListener = fxListener;
            this.fileOperator = fileOperator;
            this.worldName = worldName;
            this.randomGenerator = new Random(randomSeed);
        }

        public void SetChunkManager(ChunkManager chunkManager)
        {
            this.chunkManager = chunkManager;
        }

        public GeneratorProcess Generate(Config config)
        {
            this.sizeX = 1 << config.worldSize.worldSizeBitsX;
            this.sizeY = 1 << config.worldSize.worldSizeBitsY;
            this.sizeZ = 1 << config.worldSize.worldSizeBitsZ;

            gameplay = config.gameplay.gameplay;
            gameplay.Init(this);

            return gameplay.Generate(config);
        }


        public void Update(float deltaTime)
        {
            chunkManager.Update(deltaTime);
        }
    }
}
